<?php
// file_put_contents('log.txt',date('H:i:s')."123".'\n',FILE_APPEND);exit; //测试是否写入正常(检查目录是否可写)
define(TOKEN, 'ML168');
$obj = new Weixin();

if(!isset($_GET['echostr'])){
    $obj->receive();
}else{
    $obj->checkSignature();  
}
// $obj->checkSignature();
class Weixin {
	public function checkSignature(){
		$signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			echo $_GET['echostr'];
		}else{
			return false;
		}
	}

    public function receive(){
        $obj = $GLOBALS['HTTP_RAW_POST_DATA'];
        // $obj = 'add test';

        $postSql = simplexml_load_string($obj,'SimpleXMLElement',LIBXML_NOCDATA);

        $this->logger("\n接受：\n".$obj);

        if(!empty($postSql)){
            switch (trim($postSql->MsgType)) {
                case 'text':
                    $result = $this->receiveText($postSql);
                    if(!empty($result)){
                        echo $result;
                    }else{
                        $xml = "<xml>
                                 <ToUserName><![CDATA[%s]]></ToUserName>
                                 <FromUserName><![CDATA[%s]]></FromUserName> 
                                 <CreateTime>%s</CreateTime>
                                 <MsgType><![CDATA[%s]]></MsgType>
                                 <Content><![CDATA[%s]]></Content>
                                </xml>";

                        echo $result = sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,'没有这条文本消息');
                    }
                    break;
            }
        }

    }

    private function receiveText($postSql){
        $content = trim($postSql->Content);

        //模糊查找
        if(strstr($content,"你好")){
            $xml = "<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName> 
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                    </xml>";

            $result = sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,'hello');

            return $result;
        }

    }

    private function logger($content){
        $logSize = 10000;
        $log = 'log.txt';

        if(file_exists($log) && filesize($log) > $logSize){
            unlink($log);
        }

        file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND); // "\n" 用双引号，单引号不识别
    }

}